package com.wfh.picture.manager.websocket;

import cn.hutool.core.util.StrUtil;
import com.wfh.picture.manager.auth.SpaceUserAuthManager;
import com.wfh.picture.manager.auth.model.SpaceUserPermissionConstant;
import com.wfh.picture.model.entity.Picture;
import com.wfh.picture.model.entity.Space;
import com.wfh.picture.model.entity.User;
import com.wfh.picture.model.enums.SpaceTypeEnum;
import com.wfh.picture.service.PictureService;
import com.wfh.picture.service.SpaceService;
import com.wfh.picture.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author FengHuan Wang
 * @Date 2025/5/18 9:16
 * @Version 1.0
 */
@Slf4j
@Component
public class WsHandShakeInterceptor implements HandshakeInterceptor {


    @Resource
    private UserService userService;

    @Resource
    private PictureService pictureService;

    @Resource
    private SpaceUserAuthManager spaceUserAuthManager;

    @Resource
    private SpaceService spaceService;

    /**
     * websocket
     * @param request
     * @param response
     * @param wsHandler
     * @param attributes
     * @return
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        // 获取到当前登录用户
        if (request instanceof ServletServerHttpRequest){
            HttpServletRequest servletRequest = (HttpServletRequest) request;
            String pictureId = servletRequest.getParameter("pictureId");
            if (StrUtil.isBlank(pictureId)){
                return false;
            }
            User loginUser = userService.getLoginUser(servletRequest);
            if (loginUser == null){
                return false;
            }
            // 校验用户是否有编辑图片的权限
            Picture picture = pictureService.getById(pictureId);
            if (picture == null){
                return false;
            }
            Long spaceId = picture.getSpaceId();
            Space space = null;
            if (spaceId != null){
                space = spaceService.getById(spaceId);
                if (space == null){
                    return false;
                }

                if (space.getSpaceType() != SpaceTypeEnum.TEAM.getValue()){
                    return false;
                }
            }
            List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);

            if (!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)){
                return false;
            }
            // 将用户信息存储到 WebSocket 的属性中，在 WebSocket 通道中可以获取到该用户信息
            attributes.put("user", loginUser);
            attributes.put("pictureId", Long.valueOf(pictureId));
            attributes.put("userId", loginUser.getId());
        }
        return false;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
